package com.dmpp.captcha;


import cn.hutool.captcha.*;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import jakarta.annotation.Resource;

/**
 * @Author 北橙
 * @Create 2022/6/13
 * @Description 验证码配置类
 */
@Configuration
@ConditionalOnClass({CaptchaService.class})
@EnableConfigurationProperties({CaptchaProperties.class})
@Import(CaptchaController.class)
public class CaptchaAutoConfiguration {

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private CaptchaProperties captchaProperties;

    @Bean
    public CaptchaService captchaService() {
        AbstractCaptcha captcha;
        //创建GIF验证码
        if (CaptchaType.LineCaptcha.equals(captchaProperties.getType())) {
            captcha = new LineCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight(), captchaProperties.getCodeCount(), 15);
        } else if (CaptchaType.CircleCaptcha.equals(captchaProperties.getType())) {
            captcha = new CircleCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight(), captchaProperties.getCodeCount(),15);
        } else if (CaptchaType.ShearCaptcha.equals(captchaProperties.getType())) {
            captcha = new ShearCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight(), captchaProperties.getCodeCount(),3);
        } else if (CaptchaType.GifCaptcha.equals(captchaProperties.getType())) {
            captcha = new GifCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight(), captchaProperties.getCodeCount(), 15);
        } else if (CaptchaType.RandomCaptcha.equals(captchaProperties.getType())) {
            captcha = new RandomCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight(), captchaProperties.getCodeCount(), 15);
        } else {
            throw new NullPointerException("验证码生成器类型错误");
        }
        return new CaptchaServiceImpl(captchaProperties, redissonClient, captcha);
    }
}
